<?php
ob_start();
require_once("connect.php");
require_once("misc.php");
if(isset($_GET['u'])){
	$u = $_GET['u'];
	$fullUrl = getFullUrl($u);
	if($fullUrl == ""){
		echo "<div id='alert'>URL Cannot be found</div>";
		die;
	}else{
		header("location:$fullUrl");
		die;
	}
}

if(logged()){ 
	//go to user form if logged in
	header("Location:user.php"); die;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Kristo URL Shortener</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
	<link rel="stylesheet" href="src/style.css">
	<link rel="icon" type="image/png" href="icon.png">
	
	<script src="bootstrap/js/bootstrap.min.js"></script>
	<script src="src/jquery-2.1.4.js"></script>
	<script type="text/javascript">
	function showForm(id){
		//hide other
		$("div[id=login]").hide();
		$("div[id=regis]").hide();
		$("div[id=shorten]").hide();
		$("div[id="+id+"]").show();
	}
	function goToUser(user,id){
		$("input[name=userSession]").val(user);
		$("input[name=userNoSession]").val(id);
		$("form[name=gotoUser]").submit();
	}
	function parseResult(result,infoSuccess,infoFailed,li){
		result = trim(result);
		if(result.substring(0,7) == "success"){
			li.text(infoSuccess+" successful, redirecting..");
			$("input[name=pass]").val("");
			var userObj = result.split("---");
			goToUser(userObj[1],userObj[2]);
		}else{
			li.text(infoFailed);
			enableBtn("",true);
		}
	}
	function login(){
		enableBtn("",false);
		var li = $("div[id=loginInfo]");
		li.text("Logging in..");
		var user = $("input[name=uname]").val();
		var pass = $("input[name=pass]").val();
		$.post("checkLogin.php",{user:user, pass:pass}, function(result){
			parseResult(result,"Login","Login failed, check again..",li);
		});
	}
	function regis(){
		var li = $("div[id=regInfo]");
		var user = trim($("input[name=regname]").val());
		var pass = $("input[name=regpass]").val();
		var cpass = $("input[name=regcpass]").val();
		if(user == ""){
			li.text("Username cannot be empty"); return;
		}else if(trim(pass) == ""){
			li.text("Password cannot be empty"); return;
		}else if(pass != cpass){
			li.text("Password and confirmation is not same, check again"); return;
		}
		enableBtn("",false);
		$.post("checkReg.php",{user:user, pass:pass}, function(result){
			parseResult(result,"Registration","Registration failed, username exists..",li);
		});
	}
	$(function(){
		//restrict username field
		$("input[name$=name]").bind('keypress', function(e){
			var regex = new RegExp("^[a-zA-Z0-9._]+$");
			var key = String.fromCharCode(!e.charCode ? e.which : e.charCode);
			if (!regex.test(key)) {
			   e.preventDefault();
			   return false;
			}
		});
	});
	function refresh(){}
	</script>
</head>
<body>
	<h4>Kristo URL Shortener</h4>
	<h6>This app relies on Javascript, please enable it in your browser</h6>
	<div class="form-group">
	<button name="loginShowBtn" onclick="showForm('login')">Login</button>
	<button name="regisShowBtn" onclick="showForm('regis')">Registration</button>
	<button name="shortShowBtn" onclick="showForm('shorten')">Quick URL Shorten</button>
	<div id="login" class="form-group" style="display:none">
		<br>
		<h5>User Login</h5>
		Username : <input type="text" style="width:230px" name="uname" class="form-control" maxlength=20>
		Password : <input type="password" style="width:230px" name="pass" class="form-control" maxlength=20>
		<br><button name="loginBtn" onclick="login()">Login</button>
		<div id="loginInfo"></div>
	</div>
	<div id="regis" class="form-group" style="display:none">
		<br>
		<h5>User Registration</h5>
		Username : <input type="text" style="width:230px" name="regname" class="form-control" maxlength=20>
		Password : <input type="password" style="width:230px" name="regpass" class="form-control" maxlength=20>
		Confirm Password : <input type="password" style="width:230px" name="regcpass" class="form-control" maxlength=20>
		<br><button name="regBtn" onclick="regis()">Register</button>	
		<div id="regInfo"></div>
	</div>
	</div>
	<?php include("shorten.php"); ?>
	<div id="content">
	</div>
	
	<form method="post" name="gotoUser" action="userLogged.php">
		<input type="hidden" name="userSession">
		<input type="hidden" name="userNoSession">
	</form>
</body>

</html>

<?php ob_end_flush(); ?>